/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    Hero_Finances_master
 *    
 *    ShowLicenseActivity
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2011-8-19
 *    @version: 1.0
 *
 ******************************************************************************/

package com.waps.util;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.tool.finances.lifestyle.R;
import com.waps.AppConnect;
import com.waps.UpdatePointsNotifier;

/**
 * ShowLicenseActivity of Hero_Finances_master.
 * 
 * @author zuochen
 * 
 */

public class ShowLicenseActivity extends Activity implements OnClickListener,
		UpdatePointsNotifier {
	private Button btnMoreAPP;
	private Button btnGetPoint;

	private static Activity mActivity;
	public static void showLicenseDialog(Activity activity) {
		final Intent intent = new Intent(activity, ShowLicenseActivity.class);
		activity.startActivity(intent);
		mActivity = activity;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_license);
		setTitle("温馨提醒");
		initContentView();
	}

	private void initContentView() {
		btnMoreAPP = (Button) findViewById(R.id.btn_more_app);
		btnGetPoint = (Button) findViewById(R.id.btn_point);
		btnMoreAPP.setOnClickListener(this);
		btnGetPoint.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_more_app:
			AppConnect.getInstance(ShowLicenseActivity.this).showOffers(
					ShowLicenseActivity.this);
			break;
		case R.id.btn_point:
			AppConnect.getInstance(ShowLicenseActivity.this).getPoints(
					ShowLicenseActivity.this);
			break;
		default:
			break;
		}
	}

	private int points = 0;
	@Override
	public void getUpdatePoints(String arg0, int arg1) {
		points = arg1;
		RateUSModel.getInstance(ShowLicenseActivity.this).saveUserPoint(arg1);
		mHandler.sendEmptyMessage(MSG_LOAD_PIONTS_SUCCESSFULLY);
	}

	@Override
	public void getUpdatePointsFailed(String arg0) {
		mHandler.sendEmptyMessage(MSG_LOAD_PIONTS_FAILED);
	}

	private static final int MSG_LOAD_PIONTS_FAILED = 20;
	private static final int MSG_LOAD_PIONTS_SUCCESSFULLY = 21;
	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_LOAD_PIONTS_FAILED:
				Toast.makeText(ShowLicenseActivity.this, "获取积分失败,请检查你的网络!",
						Toast.LENGTH_LONG).show();
				break;
			case MSG_LOAD_PIONTS_SUCCESSFULLY:
				Toast.makeText(ShowLicenseActivity.this, "您当前的积分为：" + points,
						Toast.LENGTH_LONG).show();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			if (null!=mActivity) {
				mActivity.finish();
			}
			android.os.Process.killProcess(android.os.Process.myPid());
	        System.exit(10);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}}
